package org.minuframework.file.upload.spring;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.minuframework.exception.ExceptionFailed;
import org.minuframework.file.upload.AbstractFileBuilder;
import org.minuframework.file.upload.FileParamerConfigurer;
import org.minuframework.file.upload.FileTypesConfigurer;
import org.minuframework.file.upload.FileUploadType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.google.common.collect.Lists;

@Deprecated
public class DefaultFileUpload extends AbstractFileBuilder implements FileUploadFactory {
    public DefaultFileUpload(FileParamerConfigurer fileParamerConfigurer, FileTypesConfigurer fileTypesConfigurer, String charset) {
        super(fileParamerConfigurer, fileTypesConfigurer, charset);
    }
    
    public String fileUpload(HttpServletRequest request, FileUploadType type) throws ExceptionFailed {
        StringBuilder result = new StringBuilder();
        if (ServletFileUpload.isMultipartContent(request)) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) WebUtils
                    .getNativeRequest(request, MultipartHttpServletRequest.class);
            Iterator<String> it = multipartRequest.getFileNames();
            while (it.hasNext()) {
                MultipartFile file = multipartRequest.getFile((String) it.next());
                
                String param = fileUpload(file, type);
                result.append(param);
            }
        }
        return result.toString();
    }
    
    public List<String> fileUploadList(HttpServletRequest request, FileUploadType type) throws ExceptionFailed {
        List<String> realpathlist = Lists.newArrayList();
        if (ServletFileUpload.isMultipartContent(request)) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) WebUtils
                    .getNativeRequest(request, MultipartHttpServletRequest.class);
            Iterator<String> it = multipartRequest.getFileNames();
            while (it.hasNext()) {
                MultipartFile file = multipartRequest.getFile((String) it.next());
                
                String realpath = fileUpload(file, type);
                realpathlist.add(realpath);
            }
        }
        return realpathlist;
    }
    
    public String fileUpload(MultipartFile file, FileUploadType type) throws ExceptionFailed {
        StringBuilder result = new StringBuilder();
        
        File filepath = getFile(file.getOriginalFilename(), type);
        if (isFile(filepath)) {
            try {
                String realName = randomUUId(file.getOriginalFilename());
                File realpath = new File(filepath, realName);
                
                FileUtils.copyInputStreamToFile(file.getInputStream(), realpath);
                result.append(realName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            throw new ExceptionFailed("Cannot find folder from " + filepath);
        }
        return result.toString();
    }
    
    public List<String> fileUpload(List<MultipartFile> files, FileUploadType type) throws ExceptionFailed {
        List<String> result = Lists.newArrayList();
        
        Iterator<MultipartFile> it = files.iterator();
        while (it.hasNext()) {
            MultipartFile file = (MultipartFile) it.next();
            String param = fileUpload(file, type);
            result.add(param);
        }
        return result;
    }
}
